/*
 * @(#) PrivilegeInterService.java 2013-10-12 下午3:56:50
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.inter.wms;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.User;
import com.datasweep.compatibility.pnuts.Functions;
import com.datasweep.plantops.common.utility.EncryptionUtils;
import com.osi.mes.inter.BaseInterService;

public class PrivilegeInterService extends BaseInterService
{

	private static PrivilegeInterService instance;
	public PrivilegeInterService(Functions functions)
	{
		super(functions);
	}
	
	private PrivilegeInterService(Context context)
	{
		super(context);
	}
	
	public static PrivilegeInterService getInstance(Context context)
	{
		if (instance == null)
		{
			synchronized (PrivilegeInterService.class)
			{
				if (instance == null)
				{
					instance = new PrivilegeInterService(context);
				}
			}
		}
		return instance;
	}
	
	public String[] validateUserPriviliege(String xmlStr)
	{
		String[] results = new String[2];
		String result = "0";
		String[] datas = xmlStr.split(",");
		String userName = datas[0];
		String password = datas[1];
		String privilege = datas[2];
		String remark = null;
		User user = getFunctions().getUser(userName);
		if(user != null)
		{
			String sendPassword = EncryptionUtils.getEncryptedPassword(password,false);
			System.out.println(sendPassword);
			if(user.getPassword().equals(sendPassword))
			{
				if(user.hasPrivilege(privilege))
				{
					result = "1";
				}
				else
				{
					remark = "该用户没有此权限";
				}
			}
			else
			{
				remark = "该用户密码错误";
			}
		}
		else
		{
			remark = "该用户不存在";
		}
		results[0] = "<MessageResult><Status>"+result+"</Status><LastErrMsg>"+remark+"</LastErrMsg></MessageResult>";
		return results;
	}

}
